﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HumanResources
{
    public abstract class TaxEntity 
        : Object, Payable
    {
        public delegate void Notification(String prop, Object value);

        private string name;
        private string taxid;
      
        public event Notification PropertyChangeEvent;
        public event Notification PayEvent;

        public TaxEntity(string name, string taxid)
            : base()
        {
            Name = name;
            TaxId = taxid;
        }
 
        protected void TriggerPropertyChangeEvent(String name, Object p)
        {
            if (PropertyChangeEvent != null)
                PropertyChangeEvent(name, p);
        }
        protected void TriggerPayEvent(String name, Object p)
        {
            if (PayEvent != null)
                PayEvent(name, p);
        }

        public string Name
        {
            get { return name; }
            set
            {
                if (value != null && value.Matches(@"^[\w\s]{2,30}$"))
                {
                    name = value;
                    TriggerPropertyChangeEvent("Name", value);
                }
                else
                {
                    throw new ValidationException("Name", value);
                // never gets here!
                }
            }
        }
        public  string TaxId
        {
            get { return taxid; }
            set
            {
                if (value != null && value.Matches(TaxIdRegularExpression))
                {
                    taxid = value;
                    TriggerPropertyChangeEvent("TaxId", value);
                }
                else
                    throw new ValidationException("TaxId", value);

            }
        } 

        protected abstract string TaxIdRegularExpression
        {
            get;
        }

        public virtual float Pay()
        {
            TriggerPayEvent(Name, this);
            return 0;
        }
    }
}
